package com.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dao.ProfessionDao;
import com.example.data.request.ReqProfessionList;
import com.example.entity.Profession;
import com.example.service.ProfessionService;
import com.example.utils.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author:yanghua
 * Date:2023/10/7 18:05
 */
@Service
public class ProfessionServiceImpl extends ServiceImpl<ProfessionDao, Profession> implements ProfessionService{
    @Resource
    private ProfessionDao professionDao;

    @Override
    public R addOneProfession(Profession profession) {
        // 判重
        String professionName = profession.getProfession();
        List<Profession> allProfessions = professionDao.getAllProfessions();
        for (Profession p : allProfessions) {
            if (p.getProfession().equals(professionName)) {
                return R.error("专业已存在！");
            }
        }
        int i = professionDao.insert(profession);
        Map<String, Object> data = new HashMap<>();
        data.put("info", profession);
        return i == 1 ? R.ok(data,"添加成功") : R.error("添加失败");
    }

    @Override
    public R deleteOneProfession(Long id) {
        Integer i = professionDao.deleteOneProfession(id);
        return i == 1 ? R.ok("删除成功") : R.error("删除失败");
    }

    @Override
    public R updateProfession(Profession profession) {
        int i = professionDao.updateById(profession);
        Map<String, Object> data = new HashMap<>();
        data.put("info", profession);
        return i == 1 ? R.ok(data,"修改成功") : R.error("修改失败");
    }

    @Override
    public R getProfessionList(ReqProfessionList reqProfessionList) {
        reqProfessionList.setPage((reqProfessionList.getPage() - 1) * reqProfessionList.getSize());
        List<Profession> professionList = professionDao.getProfessionList(reqProfessionList);
        Integer number = professionDao.getProfessionNum(reqProfessionList);
        Map<String, Object> data = new HashMap<>();
        data.put("list", professionList);
        data.put("size", number);
        return R.ok(data, "查询成功");
    }
}
